package com.wanghang89.gmail.http;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.log4j.Logger;

public class URLParse {
	
	private final static Logger LOGGER = Logger.getLogger(URLParse.class);
	
	public static void parse(String url , String encoding) {
		String nu;
		try {
			nu = URLDecoder.decode(url,encoding);
			System.out.println(nu);
			String[] params = nu.split("&");
			for (int i = 0; i < params.length; i++) {
				System.out.println(params[i]);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	public static void show(String url ) {
		show(url,"utf-8");
	}
	public static void show(String url , String encoding) {
		String nu;
		try {
			nu = URLDecoder.decode(url,encoding);
			String[] nus = nu.split("\\?");
			String[] params = (nus.length>1?nus[1].split("&"):nu.split("&"));
			for (int i = 0; i < params.length; i++) {
				String str  =params[i];
				String[] arr = str.split("=");
				System.out.println("private final static String INPUT_"+arr[0]+" =\""+str+"\";");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	public static String encode(String str ){
		return encode(str,"utf-8");
	}
	
	public static String encode(String str , String encoding)
	{
		if (encoding==null||encoding.trim().isEmpty()) {
			return str;
		}
		try {
			return URLEncoder.encode(str,encoding);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	public static void parse(String url)
	{
		parse(url,"utf-8");
	}
	
	public static void main(String[] args) {
	}
}
